FrameLib  0.1
Arbitrarily timed and sized frame-based DSP
FrameLib_Global.h
Go to the documentation of this file.
1 
2 #ifndef FRAMELIB_GLOBAL_H
3 #define FRAMELIB_GLOBAL_H
4 
5 #include "FrameLib_Memory.h"
7 #include "FrameLib_DSPQueue.h"
8 #include "FrameLib_Threading.h"
9 #include <vector>
10 
11 // A template class for storing reference counted pointers against reference addresses (representing contexts)
12 
13 template <class T> class FrameLib_PointerSet
14 {
15  // A simple countable pointer with a reference address
16 
17  template <class U> struct CountablePointer
18  {
19  CountablePointer(T* object, void *reference) : mObject(object), mReference(reference), mCount(1) {}
20 
21  T *mObject;
22  void *mReference;
23  long mCount;
24  };
25 
26  // Type definition for concision
27 
28  typedef std::vector<CountablePointer<T> > VectorType;
29 
30 public:
31 
32  // Find a pre-existing object by reference address
33 
34  T *find(void *reference)
35  {
36  for (typename VectorType::iterator it = mPointers.begin(); it != mPointers.end(); it++)
37  {
38  if (it->mReference == reference)
39  {
40  it->mCount++;
41  return it->mObject;
42  }
43  }
44 
45  return NULL;
46  }
47 
48  // Release a pre-existing object by reference address
49 
50  void release(void *reference)
51  {
52  for (typename VectorType::iterator it = mPointers.begin(); it != mPointers.end(); it++)
53  {
54  if (it->mReference == reference)
55  {
56  if (--it->mCount < 1)
57  {
58  delete it->mObject;
59  mPointers.erase(it);
60  }
61 
62  return;
63  }
64  }
65  }
66 
67  // Add an object given a pointer (transferring ownership) and a reference address
68 
69  void add(T *object, void *reference)
70  {
71  mPointers.push_back(CountablePointer<T>(object, reference));
72  }
73 
74 private:
75 
76  // Internal set of pointers
77 
78  std::vector<CountablePointer<T> > mPointers;
79 };
80 
81 // ************************************************************************************** //
82 
83 // The Global Object
84 
86 {
87 
88 private:
89 
90  // Constructor / Destructor
91 
92  FrameLib_Global() : mCount(0) {}
93  ~FrameLib_Global() {};
94 
95  // Deleted
96 
98  FrameLib_Global& operator=(const FrameLib_Global&);
99 
100 public:
101 
102  // Retrieve and release the global object
103 
104  static FrameLib_Global *get(FrameLib_Global **global);
105  static void release(FrameLib_Global **global);
106 
107  // Methods to retrieve common objects
108 
109  FrameLib_LocalAllocator *getAllocator(void *reference);
110  FrameLib_ConnectionQueue *getConnectionQueue(void *reference);
111  FrameLib_DSPQueue *getDSPQueue(void *reference);
112 
113  // Methods to release common objects
114 
115  void releaseAllocator(void *reference);
116  void releaseConnectionQueue(void *reference);
117  void releaseDSPQueue(void *reference);
118 
119 private:
120 
121  // Reference Counting / Auto-deletion
122 
123  void increment();
124  FrameLib_Global *decrement();
125 
126  // Common Objects
127 
128  FrameLib_GlobalAllocator mAllocator;
129 
133 
134  FrameLib_SpinLock mLock;
135  long mCount;
136 };
137 
138 #endif
Definition: FrameLib_Global.h:85
Definition: FrameLib_ConnectionQueue.h:6
Definition: FrameLib_Threading.h:124
void add(T *object, void *reference)
Definition: FrameLib_Global.h:69
Definition: FrameLib_Global.h:13
Definition: FrameLib_Memory.h:14
T * find(void *reference)
Definition: FrameLib_Global.h:34
Definition: FrameLib_Memory.h:197
void release(void *reference)
Definition: FrameLib_Global.h:50
Definition: FrameLib_DSPQueue.h:12